package tech.jdbc;

import java.sql.*;

/**
 * Create with Intellij IDEA
 *
 * @Author Q
 * @date 2021/11/25/9:20
 */
public class HelloWorld {
    public static void main(String[] args) {
        //
        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;
        //1.注册驱动，但从jdbc4.x规范开始，可以省略不写
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url="jdbc:mysql://localhost:3306/baby?useSSl=false";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url,user,password);

            st = conn.createStatement();

            String sql = "select id sid,first_name,salary from s_emp";

            rs = st.executeQuery(sql);

            while (rs.next()){

                Integer id = rs.getInt("sid");
                String name = rs.getString(2);
                Double salary = rs.getDouble(3);

                System.out.println(id+":"+name+":"+salary);
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if(null!=rs){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(null!=st){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(null!=conn){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
